//
//  TGCustomAnnotationView.m
//  GroupBuy
//
//  Created by wwj on 15/11/19.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGCustomAnnotationView.h"
#import "TGAnnotationModel.h"
#import "TGCustomCalloutView.h"
#import "TGBusinessModel.h"
#import "TGDealModel.h"

@interface TGCustomAnnotationView ()
@property (strong, nonatomic) TGCustomCalloutView *calloutView;
@end

@implementation TGCustomAnnotationView

+(instancetype)customAnnotationViewWithMapView:(MAMapView *)mapView viewForAnnotation:(TGAnnotationModel *)annotation{
    static NSString *reuseIndetifier = @"annotationReuseIndetifier";
    TGCustomAnnotationView *annotationView = (TGCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
    if (annotationView == nil){
        annotationView = [[TGCustomAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:reuseIndetifier];
    }
    annotationView.annotation = annotation;
    annotationView.image = [UIImage imageNamed:annotation.icon];
    //annotationView.image = [UIImage imageNamed:@"icon_map_cateid_1"];
    annotationView.centerOffset = CGPointMake(0, -18);
    annotationView.canShowCallout = NO;
    return annotationView;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    if (self.selected == selected){
        return;
    }
    
    if (selected){
        if (self.calloutView == nil){
            self.calloutView = [TGCustomCalloutView customCalloutView];
            self.calloutView.frame = CGRectMake(0, 0, _calloutView.frame.size.width, _calloutView.frame.size.height);
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
        }
        TGAnnotationModel *aModel = self.annotation;
        TGBusinessModel *model = aModel.business;
        model.image = [aModel.deal s_image_url];
        self.calloutView.bmodel = model;
        
        [self addSubview:self.calloutView];
    }
    else{
        [self.calloutView removeFromSuperview];
    }
    [super setSelected:selected animated:animated];
}

@end
